﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Sdari.Api.DbModel;
using System.Threading.Tasks;

namespace Sdari.Api.Gateway.Controllers
{
    [Authorize]
    public class TokenInfoController : Controller
    {
        private readonly ITokenServiceContext _db;
        private readonly ILogger<TokenInfoController> _logger;

        public TokenInfoController(ITokenServiceContext context, ILogger<TokenInfoController> logger)
        {
            _db = context;
            _logger = logger;
        }

        // GET: TokenInfoes
        public async Task<IActionResult> Index()
        {
            var tokenDbConnection = _db.TokenInfo.Include(t => t.ConsumerU);
            return View(await tokenDbConnection.ToListAsync());
        }

        /*
        // GET: TokenInfoes/Details/5
        public async Task<IActionResult> Details(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var tokenInfo = await _context.TokenInfos
                .Include(t => t.ConsumerU)
                .FirstOrDefaultAsync(m => m.Id == id);
            if (tokenInfo == null)
            {
                return NotFound();
            }

            return View(tokenInfo);
        }

        // GET: TokenInfoes/Create
        public IActionResult Create()
        {
            ViewData["ConsumerUid"] = new SelectList(_context.ConsumerInfos, "Uid", "Secret");
            return View();
        }

        // POST: TokenInfoes/Create
        // To protect from overposting attacks, enable the specific properties you want to bind to.
        // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("Id,Token,ConsumerUid,Expire,Description,Creator,CreateDate,LastUpdater,LastUpdatedDate")] TokenInfo tokenInfo)
        {
            if (ModelState.IsValid)
            {
                _context.Add(tokenInfo);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            ViewData["ConsumerUid"] = new SelectList(_context.ConsumerInfos, "Uid", "Secret", tokenInfo.ConsumerUid);
            return View(tokenInfo);
        }

        // GET: TokenInfoes/Edit/5
        public async Task<IActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var tokenInfo = await _context.TokenInfos.FindAsync(id);
            if (tokenInfo == null)
            {
                return NotFound();
            }
            ViewData["ConsumerUid"] = new SelectList(_context.ConsumerInfos, "Uid", "Secret", tokenInfo.ConsumerUid);
            return View(tokenInfo);
        }

        // POST: TokenInfoes/Edit/5
        // To protect from overposting attacks, enable the specific properties you want to bind to.
        // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(int id, [Bind("Id,Token,ConsumerUid,Expire,Description,Creator,CreateDate,LastUpdater,LastUpdatedDate")] TokenInfo tokenInfo)
        {
            if (id != tokenInfo.Id)
            {
                return NotFound();
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(tokenInfo);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!TokenInfoExists(tokenInfo.Id))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
                return RedirectToAction(nameof(Index));
            }
            ViewData["ConsumerUid"] = new SelectList(_context.ConsumerInfos, "Uid", "Secret", tokenInfo.ConsumerUid);
            return View(tokenInfo);
        }

        // GET: TokenInfoes/Delete/5
        public async Task<IActionResult> Delete(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var tokenInfo = await _context.TokenInfos
                .Include(t => t.ConsumerU)
                .FirstOrDefaultAsync(m => m.Id == id);
            if (tokenInfo == null)
            {
                return NotFound();
            }

            return View(tokenInfo);
        }

        // POST: TokenInfoes/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> DeleteConfirmed(int id)
        {
            var tokenInfo = await _context.TokenInfos.FindAsync(id);
            _context.TokenInfos.Remove(tokenInfo);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }

        private bool TokenInfoExists(int id)
        {
            return _context.TokenInfos.Any(e => e.Id == id);
        }

        */
    }
}
